package com.yyl.fmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyl.fmall.product.entity.ProductComments;
import com.yyl.fmall.product.mapper.ProductCommentsMapper;
import com.yyl.fmall.product.service.IProductCommentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyl.fmall.product.vo.ProductCommentsCountVo;
import com.yyl.fmall.product.vo.ProductDetailCommentsVo;
import com.yyl.fmall.user.entity.User;
import com.yyl.fmall.user.service.IUserService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 商品评价  服务实现类
 * </p>
 *
 * @author YuanSir
 * @since 2022-11-07
 */
@Service
public class ProductCommentsServiceImpl extends ServiceImpl<ProductCommentsMapper, ProductComments> implements IProductCommentsService {
    
    @Autowired
    private IUserService userService;
    
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public ProductCommentsCountVo getDetailCommentCount(String productId) {
    
        //查询所有该商品评论信息
        List<ProductComments> productCommentsList = list(new LambdaQueryWrapper<ProductComments>()
                .eq(ProductComments::getProductId, productId));
    
        Long goodTotal = productCommentsList.stream()
                .filter(productComments -> productComments.getCommType() == 1)
                .count();
    
        Long midTotal = productCommentsList.stream()
                .filter(productComments -> productComments.getCommType() == 0)
                .count();
    
        Long badTotal = productCommentsList.stream()
                .filter(productComments -> productComments.getCommType() == -1)
                .count();
    
        long total = productCommentsList.size();
        
        BigDecimal percentBigDecimal = new BigDecimal(goodTotal)
                .multiply(new BigDecimal(100))
                .divide(new BigDecimal(total), 2, RoundingMode.HALF_UP);
    
        String percent = percentBigDecimal.toString();
    
        return new ProductCommentsCountVo(goodTotal, total, badTotal,midTotal, percent);
    }
    
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public ProductDetailCommentsVo getDetailComments(String productId, Long pageNum, Long limit) {
        //创建分页对象
        Page<ProductComments> productCommentsPage = new Page<>(pageNum, limit);
        
        //分页查询（带条件）
        page(productCommentsPage, new LambdaQueryWrapper<ProductComments>()
                .eq(ProductComments::getProductId, productId));
    
        //获取记录总数
        Long count = productCommentsPage.getTotal();
    
        //获取总页数
        Long pageCount = productCommentsPage.getPages();
    
        //获取分页查询后的数据
        List<ProductComments> productCommentsList = productCommentsPage.getRecords();
        
        populateProductCommentsList(productCommentsList);
    
        //返回vo类对象（前端所需要的json数据中的data数据）
        return new ProductDetailCommentsVo(pageCount, count, productCommentsList);
    }
    
    /**
     * 把缺少的用户信息的productCommentsList补全用户信息
     * @param productCommentsList
     */
    private void populateProductCommentsList(List<ProductComments> productCommentsList) {
    
        List<Integer> userIdList = productCommentsList.stream()
                .map(productComments -> Integer.valueOf(productComments.getUserId()))
                .collect(Collectors.toList());
    
        List<User> userList = userService.list(new LambdaQueryWrapper<User>()
                .in(User::getUserId, userIdList));
    
        Map<Integer, User> userIdMap = userList.stream()
                .collect(Collectors.toMap(User::getUserId, user -> user));
    
        //遍历productCommentsList，往每一个评论对象中插入评论人信息user
        productCommentsList.forEach(productComments -> {
            User user = userIdMap.get(Integer.valueOf(productComments.getUserId()));
    
            //在userList中查找userId对应的user对象
    
            productComments.setUsername(user.getUsername());
        
            productComments.setNickname(user.getNickname());
        
            productComments.setUserImg(user.getUserImg());
        
        });
    }
}
